﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.NguoiDung;

namespace TimViec.NguoiDung.UserControls
{
    public partial class wuc_TuHoSo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            if (Session["NguoiDung"] != null)
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                if (user != null)
                {
                    var lstHoSo = HoSo.GetHoSoByUserId(user.UserId);
                    rptHoSo.DataSource = lstHoSo;
                    rptHoSo.DataBind();
                }
            }
            else
            {
                Response.Redirect("/");
            }
        }

        protected void rptHoSo_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Edit")
            {
                int hosoId = int.Parse(e.CommandArgument.ToString());
                Response.Redirect("/nguoi-dung/"+hosoId+"tao-ho-so-buoc6.html");
                return;
            }

            if(e.CommandName=="Xoa")
            {
                DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
                int hosoId = int.Parse(e.CommandArgument.ToString());
                BLL.NguoiDung.HoSo.XoaHoSo(hosoId);
                var lstHoSo = HoSo.GetHoSoByUserId(user.UserId);
                rptHoSo.DataSource = lstHoSo;
                rptHoSo.DataBind();
            }
        }

        protected void btTaoHoSo_Click(object sender, EventArgs e)
        {
            DAL.User user = BLL.NguoiDung.NguoiDung.Get_UserEmail(Session["NguoiDung"].ToString());
            if (BLL.NguoiDung.HoSo.CountTotalHoSo(user.UserId) < 3)
            {
                Response.Redirect("/nguoi-dung/tao-ho-so.html");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe",
                                                            "alert('Bạn không thể tạo quá 3 hồ sơ!');", true);
            }
        }

        protected void btXoaHoSo_Click(object sender, EventArgs e)
        {

        }
    }
}